package com.user.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.user.entity.User;
import com.user.service.UserService;
import com.user.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by qpx on 2017/5/11.
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/userList")
    public String getUserList(User user, ModelMap modelMap, HttpServletRequest request){
        //使用PageHelper分页分页插件
        PageHelper.startPage(user.getPage(),user.getPageSize());
        //获取分页数据
        Page<User> users = userService.getUserList(user);
        //	public static void page(HttpServletRequest request, String url, Integer pageSize, List<?> list, Integer listCount, Integer page) {
//        System.out.println("request.getContextPath():" + request.getContextPath());
//        System.out.println("request.getRequestURI():" + request.getRequestURI());
//        System.out.println("request.getRequestURL():" + request.getRequestURL());
        PageUtil.page(request,request.getRequestURI(),user.getPageSize(),users.getResult(),(int)users.getTotal(),user.getPage());
        System.out.println(JSON.toJSONString(users));
        modelMap.put("users",users);
        return "userList";
    }
}
